"""
Case Type   : 资源池化故障模式库
Case Name   : 审计文件路径无效-数据库启动前，审计日志路径配置无效（无权限）
Create At   : 2025/05
Owner       : haomeng
Description :
    1、修改操作审计开关为全开
    2、查看集群状态,停止集群
    3、修改主节点审计日志文件为不可用
    4、重启集群，查看集群状态
    5、恢复审计日志文件权限
Expect      :
    1、参数设置成功
    2、集群状态正常，停止集群常规
    3、修改主节点审计日志文件权限不可用成功
    4、重启集群成功，集群状态正常
    5、恢复审计文件权限成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SSFauleMode(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = [Node(node='PrimaryRoot'), Node(node='PrimaryDbUser')]
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()

        self.audit_param = 'audit_system_object'

    def test_ss_faule(self):
        text = '-----step1:修改操作审计开关为全开  expect:参数设置成功-----'
        self.log.info(text)

        self.log.info('-----获取审计操作默认值-----')
        self.default_value = self.com.show_param(self.audit_param)
        self.log.info(self.default_value)

        result = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                           f'{self.audit_param} = 268435455')
        self.log.info(result)
        self.assertTrue(result, '参数设置失败')

        text = '-----step2:查看集群状态，停止集群;  expect:集群状态正常，停止集群成功-----'
        self.log.info(text)

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Normal', status, '集群状态异常')

        stop = self.pri_sh.exec_cm_ctl('stop')
        self.log.info(stop)
        self.assertIn(self.constant.cm_stop_success_msg, stop, '集群停止失败')

        text = '-----step3:修改主节点pg_audit审计日志文件权限不可用目录  expect:修改成功-----'
        self.log.info(text)

        chmod = f'chmod 000 {macro.PG_AUDIT_PATH}/dn_6001/*adt;' \
                f'ls -al {macro.PG_AUDIT_PATH}/dn_6001/*adt;'
        self.log.info(chmod)

        chmod_execute = self.user_node[1].sh(chmod).result()
        self.log.info(chmod_execute)
        self.assertIn('----------', chmod_execute, '修改pg_audit目录权限失败')

        text = '-----step6:重启集群  expect:重启成功-----'
        self.log.info(text)

        restart = self.pri_sh.exec_cm_ctl('start')
        self.log.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart, '集群重启失败')

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '集群重启失败')

    def tearDown(self):
        text = '-----step8:恢复环境  expect:恢复环境成功-----'
        self.log.info(text)

        rechmod = f'chmod 600 {macro.PG_AUDIT_PATH}/dn_6001/*adt;' \
                  f'ls -al {macro.PG_AUDIT_PATH}/dn_6001/*adt;'
        self.log.info(rechmod)
        res = self.user_node[1].sh(rechmod).result()
        self.log.info(res)

        reset = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                          f'{self.audit_param} = {self.default_value}')
        self.log.info(reset)
        self.assertIn('-rw', res, 'pg_audit文件权限异常')
        self.assertTrue(reset, '参数恢复失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
